define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'share/user/index' + location.search,
                    add_url: 'share/user/add',
                    edit_url: 'share/user/edit',
                    del_url: 'share/user/del',
                    del_user_url: 'share/user/userdel',
                    multi_url: 'share/user/multi',
                    import_url: 'share/user/import',
                    addchange_url:'share/user/addchange',
                    table: 'share_user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns:true,
                fixedRightNumber:1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'user_id', title: __('User_id')},
                        {field: 'user.nickname', title: '姓名',operate:'LIKE'},
                        {field: 'user.username', title: '账号',operate:'LIKE',visible: false},
                        {field: 'user.staff', title: '测试号',operate:'LIKE',searchList: {"2":__('是')}, formatter: Table.api.formatter.is_union,visible: false},
                        {field: 'user.username_xing', title: '账号', operate:false},
                        {field: 'user.mobile', title: '手机号',operate:'LIKE',visible: false},
                        {field: 'user.mobile_xing', title: '手机号', operate:false},
                        {field: 'role', title: __('Role'), searchList: {"0":__('无'),"1":__('推广员'),"2":__('代言人'),"3":__('一星网店经销商'),"4":__('二星网店经销商'),"5":__('银牌经销商'),"6":__('金牌经销商'),"7":__('钻石经销商')}, formatter: Table.api.formatter.status},
                        {field: 'role_url', title: '网体路径',operate:'LIKE', visible: false},
                        {field: 'inviter_id', title: __('Inviter_id')},
                        // {field: 'user.invitecode', title: '邀请码',operate:false},
                        {field: 'addtime', title: __('Addtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'parent_id', title: __('Parent_id')},
                        {field: 'price', title: __('钱包'), operate: 'BETWEEN', formatter: Table.api.formatter.price},
                        {field: 'lp_price', title: __('零批余额'), operate: 'BETWEEN', formatter: Table.api.formatter.lp_price},
                        {field: 'old_price', title: __('老余额'), operate: 'BETWEEN', formatter: Table.api.formatter.old_price},
                        {field: 'advert_price', title: __('广告创收'), operate: 'BETWEEN', formatter: Table.api.formatter.advert_price},
                        {field: 'mutual_money', title: __('互助金'), operate:false},
                        {field: 'total_price', title: __('Total_price'), operate:false},
                        {field: 'amount', title: '团队人数', operate:false},
                        {field: 'score', title: '积分', operate: 'BETWEEN', formatter: Table.api.formatter.score},
                        {field: 'role_type', title: '所在团队', operate:false},
                        {field: 'role1', title: '团队1', operate:false, searchList: {"0":__('无'),"1":__('推广员'),"2":__('代言人'),"3":__('一星网店经销商'),"4":__('二星网店经销商'),"5":__('银牌经销商'),"6":__('金牌经销商'),"7":__('钻石经销商')},formatter: Table.api.formatter.status},
                        {field: 'role2', title: '团队2', operate:false,searchList: {"0":__('无'),"1":__('推广员'),"2":__('代言人'),"3":__('一星网店经销商'),"4":__('二星网店经销商'),"5":__('银牌经销商'),"6":__('金牌经销商'),"7":__('钻石经销商')}, formatter: Table.api.formatter.status},
                        {field: 'role3', title: '团队3', operate:false,searchList: {"0":__('无'),"1":__('推广员'),"2":__('代言人'),"3":__('一星网店经销商'),"4":__('二星网店经销商'),"5":__('银牌经销商'),"6":__('金牌经销商'),"7":__('钻石经销商')}, formatter: Table.api.formatter.status},
                        {field: 'is_union', title: __('联创伙伴'), searchList: {"0":__('否'),"1":__('是')}, formatter: Table.api.formatter.is_union},
                        {field: 'is_sep_income', title: __('独立负责人'), searchList: {"0":__('否'),"1":__('是')}, formatter: Table.api.formatter.is_union},
                        {field: 'is_leader', title: __('项目负责人'), searchList: {"0":__('否'),"1":__('是')}, formatter: Table.api.formatter.is_union},
                        {field: 'user_flags', title: __('分割标识')},
                        {
                            field: 'operate',
                            title: __('Operate'), table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: __('Detail'),
                                    icon: 'fa fa-list',
                                    classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                    url: 'share/user/detail'
                                },
                                {
                                    name: 'ajax',
                                    text: __('重置密码'),
                                    title: __('重置密码'),
                                    classname: 'btn btn-xs btn-info btn-magic btn-ajax',
                                    confirm: '确认重置密码为 123456 吗？',
                                    url: 'share/user/resetpwd',
                                    success: function (data, ret) {
                                        Layer.alert(ret.msg + ",返回数据：" + JSON.stringify(data));
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                                {
                                    name: '变更节点',
                                    text: __('变更节点'),
                                    icon: 'fa fa-list',
                                    classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                    url: 'share/user/change'
                                }
                                ,
                                {
                                    name: '添加节点',
                                    text: __('添加节点'),
                                    icon: 'fa fa-list',
                                    classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                    url: 'share/user/addchange'
                                },
                                {
                                    name: 'ajax',
                                    text: __('删除实名'),
                                    title: __('删除实名'),
                                    classname: 'btn btn-xs btn-info btn-magic btn-ajax',
                                    confirm: '确认删除实名和银行卡吗？',
                                    url: 'share/user/userdel',
                                    success: function (data, ret) {
                                        Layer.alert(ret.msg + ",返回数据：" + JSON.stringify(data));
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                                {
                                    name: 'ajax',
                                    text: __('禁用'),
                                    title: __('禁用'),
                                    classname: 'btn btn-xs btn-info btn-magic btn-ajax',
                                    confirm: '确认要禁用该用户吗？',
                                    url: 'share/user/disable',
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                                {
                                    name: 'ajax',
                                    text: __('取消分割'),
                                    title: __('取消分割'),
                                    classname: 'btn btn-xs btn-info btn-magic btn-ajax',
                                    confirm: '确认要取消分割吗？',
                                    url: 'share/user/unincome',
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                                {
                                    name: 'ajax',
                                    text: __('设置分割'),
                                    title: __('设置分割'),
                                    classname: 'btn btn-xs btn-info btn-magic btn-ajax',
                                    confirm: '确认要设置分割吗？',
                                    url: 'share/user/setSepTeam',
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                                {
                                    name: 'ajax',
                                    text: __('分割人资金转让'),
                                    title: __('分割人资金转让'),
                                    classname: 'btn btn-xs btn-info btn-magic btn-ajax',
                                    confirm: '确认要把下属人员资金转给独立负责人吗？',
                                    url: 'share/user/sepIncomeMoney',
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });
            table.on('load-success.bs.table',function (e,data){
                $("#all_nums").text(data.all_nums)
                $("#all_score").text(data.all_score)
                $("#all_price").text(data.all_price)
                $("#all_old_price").text(data.all_old_price)
                $("#all_lp_price").text(data.all_lp_price)
                $("#all_advert_price").text(data.all_advert_price)
                $("#all_mutual_money").text(data.all_mutual_money)
                $("#all_jin").text(data.all_jin)
                $("#all_yin").text(data.all_yin)
                $("#all_xing1").text(data.all_xing1)
                $("#all_xing2").text(data.all_xing2)
                $("#all_daiyan").text(data.all_daiyan)
                $("#all_tuiguang").text(data.all_tuiguang)
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        change: function () {
            Controller.api.bindevent();
        },
        addchange:function(){
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
